<#import "/WEB-INF/sys/js_css.html" as js>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户管理</title>
<body>
	<!-- 工具条 -->
	<div id="tools" style="padding:5px;height:auto">
		<a href="javascript:void(0)" id="btn_top_add" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加用户</a>
	</div>
	<table id="datagrid" class="easyui-datagrid" data-options="singleSelect:true,fit:true,fitColumns:true,pagination:true,
		url:'${ctx}/sys/user/search',toolbar:'#tools',rownumbers : true,pageSize:15,pageList:[5,10,15,20]">
	    <thead>
			<tr>
				<th data-options="field:'id',hidden:true">id</th><!-- hidden -->
				<th data-options="field:'action',align:'center',width:10,formatter:formatterAction">操作</th>
				<th data-options="field:'username',width:20,align:'center'">用户名</th>
				<th data-options="field:'sitename',width:35,align:'center'">管理站点</th>
				<th data-options="field:'mail',align:'center',width:20">邮箱</th>
				<th data-options="field:'regtime',align:'center',width:15">注册时间</th>
			</tr>
		</thead>
    </table>
    <!-- 添加人员信息div -->
	<div id="divpadd" class="easyui-window" title="添加用户信息" data-options="modal:true,collapsible:false,closed:true,minimizable:false,
		maximizable:false,width:460">
		<form id="topaddform" name="topaddform" method="post">
			<table class="table table-hover table-condensed">
				<tr>
					<td>用&nbsp;户&nbsp;名：<input id="addusername" name="username" type="text" class="easyui-validatebox" value="" /></td>
					<td>邮&nbsp;&nbsp;箱&nbsp;&nbsp;：<input name="mail" id="addmail" type="text" class="easyui-validatebox" value="" /></td>
				</tr>
				<tr>
					<td>密&nbsp;&nbsp;码&nbsp;&nbsp;：<input name="password" id="addpassword" type="password" class="easyui-validatebox" value="" /></td>
					<td>密码重输：<input id="addrpassword" name="rpassword" type="password" class="easyui-validatebox" value="" /></td>
				</tr>
				<tr>
					<td colspan="2">要管理的站点名称：<input name="sitename" id="addsitename" size="49" type="text" class="easyui-validatebox" value="" /></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><a href="javascript:void(0)" class="easyui-linkbutton" id="btn_window_top_add" iconCls="icon-save" plain="true">保存</a></td>
				</tr>
			</table>
		</form>
	</div>
    <!-- 修改人员密码 -->
	<div id="divpupdate" class="easyui-window" title="修改用户密码" data-options="modal:true,collapsible:false,closed:true,minimizable:false,
		maximizable:false,width:240">
		<form id="toppdateform" name="topaddform" method="post">
			<table class="table table-hover table-condensed">
				<tr>
					<td>密&nbsp;&nbsp;码&nbsp;&nbsp;：<input name="password" id="pdatepassword" type="password" class="easyui-validatebox" value="" /></td>
				</tr>
				<tr>
					<td>密码重输：<input id="pdaterpassword" name="rpassword" type="password" class="easyui-validatebox" value="" />
						<input id="updateid" name="id" value="" type="hidden" />
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center"><a href="javascript:void(0)" class="easyui-linkbutton" id="btn_window_top_pdate" iconCls="icon-save" plain="true">提交修改密码</a></td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>
<@js.easyui />
<script type="text/javascript">
jQuery(document).ready(function() {
	//添加文档按钮事件
	jQuery("#btn_top_add").click(function(){
		jQuery("#divpadd").window("open");
		jQuery("#addusername").val("");
		jQuery("#addmail").val("");
		jQuery("#addpassword").val("");
		jQuery("#addrpassword").val("");
		jQuery("#addsitename").val("");
	});
	//添加按钮事件
	jQuery("#btn_window_top_add").click(function(){
		jQuery("#topaddform").form({
			url:"${ctx}/sys/user/add",
			onSubmit:function(){
				if(!validate())
					return false;
				var servervali = false;
				jQuery.ajax({
		   			async:false,
		   			type:"post",
		   			dataType:"json",
		   			data:{username:jQuery("#addusername").val()},
		   			url:"${ctx}/sys/user/validate",
		   			success:function(date){
		   				if(!date.success){
		   					jQuery.messager.alert("信息",date.message,"error");
		   				}else
		   					servervali = true;
		   			}
		   		});
				return servervali;
			},
			success:function(data){
				if(!data){
					jQuery.messager.alert("信息","保存失败！","error");
					return;
				}
				jQuery.messager.alert("信息","保存成功！","info",function(){
					jQuery("#divpadd").window("close");
				});
				jQuery("#datagrid").datagrid("reload");
			}
		});
		//提交表单
		jQuery('#topaddform').submit();
	});
	//修改密码按钮事件
	jQuery("#btn_window_top_pdate").click(function(){
		jQuery("#toppdateform").form({
			url:"${ctx}/sys/user/upassword",
			onSubmit:function(){
				var addpassword = jQuery("#pdatepassword").val();
				var addrpassword = jQuery("#pdaterpassword").val();
				if(addpassword==""){
					jQuery.messager.alert("信息","请填密码！","info");
					return false;
				}
				if(addrpassword==""){
					jQuery.messager.alert("信息","密码重输不可为空！","info");
					return false;
				}
				if(addrpassword!=addpassword){
					jQuery.messager.alert("信息","两次输入的密码必须一致！","info");
					return false;
				}
				return true;
			},
			success:function(data){
				if(!data){
					jQuery.messager.alert("信息","修改密码失败！","error");
					return;
				}
				jQuery.messager.alert("信息","修改密码成功！","info",function(){
					jQuery("#divpupdate").window("close");
				});
			}
		});
		//提交表单
		jQuery('#toppdateform').submit();
	});
});
//验证表单信息
function validate(){
	var addusername = jQuery("#addusername").val();
	if(addusername==""){
		jQuery.messager.alert("信息","请填写用户名！","info");
		return false;
	}
	var addmail = jQuery("#addmail").val();
	if(addmail==""){
		jQuery.messager.alert("信息","请填写邮箱！","info");
		return false;
	}
	if(jQuery("#addsitename").val()==""){
		jQuery.messager.alert("信息","请填要管理的站点名称！","info");
		return false;
	}
	var addpassword = jQuery("#addpassword").val();
	var addrpassword = jQuery("#addrpassword").val();
	if(addpassword==""){
		jQuery.messager.alert("信息","请填密码！","info");
		return false;
	}
	if(addrpassword==""){
		jQuery.messager.alert("信息","密码重输不可为空！","info");
		return false;
	}
	//对用户名的验证
	var namereg = /^[0-9a-zA-Z]*$/g;
	if(!namereg.test(addusername)){
		jQuery.messager.alert("信息","用户名只能是字母或者数字！","info");
		return false;
	}
	//对电子邮件的验证
	var mailreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if(!mailreg.test(addmail)){
		jQuery.messager.alert("信息","邮箱格式不正确！","info");
		return false;
	}
	if(addrpassword!=addpassword){
		jQuery.messager.alert("信息","两次输入的密码必须一致！","info");
		return false;
	}
	return true;
}
//显示操作里的内容
function formatterAction(value,row,index){
	return "<img title='重置密码' onclick='editfun(\""+row.id+"\")' src='${ctx}/js/easyui/themes/icons/pencil.png' />&nbsp;&nbsp;"+
	"<img title='删除此用户' onclick='deletefun(\""+row.id+"\")' src='${ctx}/js/easyui/themes/icons/edit_remove.png' />";
}
//修改信息
function editfun(id){
	jQuery("#divpupdate").window("open");
	jQuery("#updateid").val(id);
	jQuery("#pdaterpassword").val("");
	jQuery("#pdatepassword").val("");
}
//删除
function deletefun(id){
	jQuery.messager.confirm("信息","删除此用户后关于此用户的所有功能将失效，您确定要删除？",function(arg){
		if(!arg)
			return;
		jQuery.ajax({
			async:false,
			type:"post",
			dataType:"json",
			data:{id:id},
			url:"${ctx}/sys/user/delete",
			success:function(date){
				if(!date.success){
					jQuery.messager.alert("信息",date.message,"error");
					return;
				}
				//刷新页面
				jQuery("#datagrid").datagrid("reload");
			}
		});
	});
}
</script>